package com.example.shiwu.service;

import android.content.Context;

import com.example.shiwu.db.dao.BoxElectrodeRoDao;
import com.example.shiwu.db.dao.base.BaseDaoFactory;
import com.example.shiwu.db.dao.base.ElectrodeRoVoDao;
import com.example.shiwu.db.model.BoxElectrodeRo;
import com.example.shiwu.db.vo.ElectrodeRoVo;
import com.example.shiwu.manager.DatabaseHelper;
import com.example.shiwu.util.DateUtils;

import java.util.ArrayList;
import java.util.List;

public class BoxElectrodeRoService {

    private DatabaseHelper dbHelper;
    private BoxElectrodeRoDao<BoxElectrodeRo> boxElectrodeRoDao;
    private ElectrodeRoVoDao<ElectrodeRoVo> boxElectrodeRoVoDao;
    private ArrayList<BoxElectrodeRo> listBoxElectrodeRo = null;

    private static final int PAGE_SIZE = 10;
    private static final int TOTAL_PAGE = 0;

    public BoxElectrodeRoService() {
    }

    public BoxElectrodeRoService(Context context) {
        dbHelper = new DatabaseHelper(context);
    }


    /**获取单个数据
     * @param boxElectrodeRo
     * @return
     */
    public List<BoxElectrodeRo> getBoxElectrodeRo(BoxElectrodeRo boxElectrodeRo) {
        boxElectrodeRoDao = BaseDaoFactory.getInstance().createBaseDao(BoxElectrodeRoDao.class, BoxElectrodeRo.class);
        listBoxElectrodeRo = new ArrayList<>();
        listBoxElectrodeRo.addAll(boxElectrodeRoDao.query(boxElectrodeRo));
        return listBoxElectrodeRo;
    }

    /**分页获取数据
     * @param boxElectrodeRo
     * @return
     */
    public List<BoxElectrodeRo> getBoxElectrodeRoList(BoxElectrodeRo boxElectrodeRo,Integer startIndex,Integer limit) {
        boxElectrodeRoDao = BaseDaoFactory.getInstance().createBaseDao(BoxElectrodeRoDao.class, BoxElectrodeRo.class);
        listBoxElectrodeRo = new ArrayList<>();
        listBoxElectrodeRo.addAll(boxElectrodeRoDao.query(boxElectrodeRo,null,"create_time desc",null,startIndex,limit));
        return listBoxElectrodeRo;
    }


    public boolean save(BoxElectrodeRo boxElectrodeRo) {
        boxElectrodeRoDao = BaseDaoFactory.getInstance().createBaseDao(BoxElectrodeRoDao.class, BoxElectrodeRo.class);
        long size = boxElectrodeRoDao.insert(boxElectrodeRo);
        if(size >0 ){
            return true;
        }
        return false;
    }
    public boolean saveOrUpdata(BoxElectrodeRo boxElectrodeRo) {
        boxElectrodeRoDao = BaseDaoFactory.getInstance().createBaseDao(BoxElectrodeRoDao.class, BoxElectrodeRo.class);
        BoxElectrodeRo boxElectrodeRo1 = new BoxElectrodeRo();
        boxElectrodeRo1.setItemId(boxElectrodeRo.getItemId());
        List<BoxElectrodeRo> lis =boxElectrodeRoDao.query(boxElectrodeRo1);
        Boolean bl = true;
        if(lis.size()>0){
            boxElectrodeRo1.setUpdateTime(DateUtils.parseDate());
            long size = boxElectrodeRoDao.update(boxElectrodeRo,new BoxElectrodeRo());
            if(size >0 ){
                return bl;
            }
            bl = false;
        }else{
            boxElectrodeRo.setCreateTime(DateUtils.parseDate());
            long size = boxElectrodeRoDao.insert(boxElectrodeRo);
            if(size >0 ){
                return bl;
            }
            bl = false;
        }

        return bl;
    }

    public boolean updade(BoxElectrodeRo BoxElectrodeRo,BoxElectrodeRo where) {
        long size = boxElectrodeRoDao.update(BoxElectrodeRo,where);
        if(size >0 ){
            return true;
        }
        return false;
    }

    public Boolean delete(BoxElectrodeRo BoxElectrodeRo) {
        int size = boxElectrodeRoDao.delete(BoxElectrodeRo);
        if(size >0 ){
            return true;
        }
        return false;
    }

    public List<ElectrodeRoVo> getAll(Integer currentPage, Integer pageSize, Integer itemId, int type){
        boxElectrodeRoVoDao = BaseDaoFactory.getInstance().createBaseDao(ElectrodeRoVoDao.class, BoxElectrodeRo.class);
        ElectrodeRoVo send = new ElectrodeRoVo();
        List<ElectrodeRoVo> lis = boxElectrodeRoVoDao.query(send,"tb_items ite left join tb_device dev on dev.item_id = ite.id left join tb_boxElectrodeRo ele on ele.item_id = ite.id ",
                new String[]{"ele.id, ite.name, ele.item_id, ite.operator, dev.sn as deviceId,ele.voltage as voltages, ele.create_time"},
                "ele.status = 0 and ite.id = ?",new String[]{String.valueOf(itemId)},"ele.id","ele.id"+" DESC","",currentPage,pageSize);
        return lis;
    }
}
